//
//  HSYCustomPhotoViewController+RACSignal.m
//  高仿暴走斗图
//
//  Created by key on 16/6/3.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYCustomPhotoViewController+RACSignal.h"
#import "NSObject+RACDescription.h"
#import "RACDelegateProxy.h"

@implementation HSYCustomPhotoViewController (RACSignal)

static void RACUseDelegateProxy(HSYCustomPhotoViewController *self) {
    
    if (self.delegate == (id)self.rac_delegateProxy) return;
    self.rac_delegateProxy.rac_proxiedDelegate = self.delegate;
    self.delegate = (id)self.rac_delegateProxy;
}

- (RACDelegateProxy *)rac_delegateProxy {
    
    RACDelegateProxy *proxy = objc_getAssociatedObject(self, _cmd);
    if (proxy == nil) {
        proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(HSYCustomPhotoViewControllerDelegate)];
        objc_setAssociatedObject(self, _cmd, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return proxy;
}

- (RACSignal *)rac_didSelect {
    
    RACSignal *signal = [[[self.rac_delegateProxy
                           signalForSelector:@selector(didSelectImage:)]
                          reduceEach:^(UIImage *image) {
        
        return image;
        
    }] setNameWithFormat:@"%@ -rac_didSelect", self.rac_description];
    
    RACUseDelegateProxy(self);
    
    return signal;
}

- (RACSignal *)rac_didSelectCompletion {

    RACSignal *siganl = [[[self.rac_delegateProxy
                           signalForSelector:@selector(didSelectCompletionImages:)]
                          reduceEach:^(NSArray *images){
        return images;
    }] setNameWithFormat:@"%@ -rac_didSelectCompletion", self.rac_description];
    RACUseDelegateProxy(self);
    
    return siganl;
}

@end
